load e:\manoj\projects\plasma\Julia_PLP.mat;
fday_list_julia = floor(fday(:,1));
fday_julia_frac = fday(1,:) - floor(fday(1,:));
bin_radius = 30/(60*24);
kp_limit = 2;
Kp_delay = 23;%Valid columns [10,13-24];
%13 - 0, 14-1,15-2,16-3,17-4,18-5,19-6,20-7,21-8,22-9,23-22,24-mean
lt_step  =  1;
lag_step = 30;

LAG = [30:lag_step:300]/(60*24);
LT = [10:lt_step:17];
ED_matrix = ones([length(LT)-1,length(LAG)])*NaN;
LV_matrix = ones([length(LT)-1,length(LAG)])*NaN;
RT_matrix = ones([length(LT)-1,length(LAG)])*NaN;
PT_matrix = ones([length(LT)-1,length(LAG)])*NaN;
ED_error = ones([length(LT)-1,length(LAG)])*NaN;
LV_error = ones([length(LT)-1,length(LAG)])*NaN;
RT_error = ones([length(LT)-1,length(LAG)])*NaN;
PT_error = ones([length(LT)-1,length(LAG)])*NaN;

N_good = 1;
for jj = 1:length(LAG),
for kk = 1:length(LT)-1,
    
     L = ~isnan(PLP(:,27)) & ~isnan(PLP(:,18))  & D_KP(:,Kp_delay) <=kp_limit & PLP(:,7) >= LT(kk) & PLP(:,7) <= LT(kk+1)...
         & PLP(:,12) == 1;% & PLP(:,20) >= 1.5;%& PLP(:,29) >= 1.5;% & PLP(:,8) > -85 & PLP(:,8) < -70;
  %  L = ~isnan(PLP(:,18))  & PLP(:,10) <=kp_limit & PLP(:,7) >= LT(kk) & PLP(:,7) <= LT(kk+1) & PLP(:,12) == 1;% & PLP(:,8) > -85 & PLP(:,8) < -70;
         IndData = PLP(L,:);
         counter = 0;
         
         for i = 1:size(IndData,1),
         fday_champ = tmjd(IndData(i,1),IndData(i,2),IndData(i,3),IndData(i,4),IndData(i,5),IndData(i,6),1);   
         iday = find(fday_list_julia==floor(fday_champ));
         if ~isempty(iday),
            fday_champ_frac =  fday_champ - floor(fday_champ) - LAG(jj);
            if fday_champ_frac > fday_julia_frac(Julia_W(iday).k(1)) & fday_champ_frac < fday_julia_frac(Julia_W(iday).k(end)),
                 counter=counter+1;
                [trash left_bin] =  min(abs(fday_julia_frac(Julia_W(iday).k(1):Julia_W(iday).k(end)) - (fday_champ_frac-bin_radius)));
                [trash right_bin] = min(abs(fday_julia_frac(Julia_W(iday).k(1):Julia_W(iday).k(end)) - (fday_champ_frac+bin_radius)));
                left_bin  = Julia_W(iday).k(1) + left_bin - 1;
                right_bin = Julia_W(iday).k(1) + right_bin -1;
                Julia_data(counter) = mean(polyval(Julia_W(iday).p,[left_bin:right_bin]));
                Plp_ed(counter) = (IndData(i,18) + IndData(i,27))/2;
              %  Plp_ed(counter) = (IndData(i,18)); %WATCH OUT ONLY ONE PEAK !
                Plp_lv(counter) = (IndData(i,17) + IndData(i,26))/2;
                %Plp_ed(counter) = (IndData(i,18) + IndData(i,27))/2;
                Plp_ratio(counter) = (IndData(i,20) + IndData(i,29))/2;
                Plp_pt(counter) = Plp_ed(counter) -IndData(i,36);
                Plp_altitude(counter) = IndData(i,9);
            end;
            
         end;
         end;
            
         lt_count(kk,jj) = counter;
        % fprintf('LT=%5d LAG = %10.5f COUNT = %3d',LT(kk),LAG(jj)*(60*24),counter); 
         if counter >= 3,
             [cof_ed,err_ed] = corrcoef(Julia_data,Plp_ed); 
             [cof_lv,err_lv] = corrcoef(Julia_data,Plp_lv); 
             [cof_rt,err_rt] = corrcoef(Julia_data,Plp_ratio); 
             [cof_pt,err_pt] = corrcoef(Julia_data,Plp_pt); 
             ED_matrix(kk,jj) = cof_ed(2,1);
             LV_matrix(kk,jj) = cof_lv(2,1);
             ED_error(kk,jj) = err_ed(2,1);
             RT_matrix(kk,jj) = cof_rt(2,1);
             RT_error(kk,jj) = err_rt(2,1);
             PT_matrix(kk,jj) = cof_pt(2,1);
             PT_error(kk,jj) = err_pt(2,1);
             LV_error(kk,jj) = err_lv(2,1);
             
%              fprintf('Coef_ed = %5.3f, err_ed = %e Coef_lv = %5.3f, err_lv = %e',cof_ed(2,1),err_ed(2,1),cof_lv(2,1),err_lv(2,1));
%               if LAG(jj)*60*24 == 120 & LT(kk) == 12,
%                   return;
%               end;
           
%              if cof_rt(2,1) < 100,
%              subplot(8,8,(jj-1)*(length(LT)-1)+kk);
%              %set(gca,'FontSize',16);
%              hold on;
%              plot(Julia_data,Plp_ratio,'r.','MarkerSize',15);
%              grid on;
%              axis([-10 50 1 3]);
%              text(30, 1.2,sprintf('%5d',LT(kk)));
%              text(30, 1.8,(max(Plp_ratio)-min(Plp_ratio))*0.3+min(Plp_ratio),sprintf('%5d',LAG(jj)*(60*24)));
%              text(30, 2.8,(max(Plp_ratio)-min(Plp_ratio))*0.8+min(Plp_ratio),sprintf('%4.2f',cof_rt(2,1)),'color','r');
%              xlabel(sprintf('%5d-%5d',LT(kk),LAG(jj)*(60*24)));
%              N_good = N_good+1;
%              end;
        end;
%         fprintf('\n');
         clear Julia_data Plp_ed Plp_lv Plp_ratio Plp_pt;
      
end;
end;

for i = 1:length(LT)-1,
SS(i,:) = sprintf('%2d-%2d',LT(i),LT(i+1));
end;

color_index = ['k*-'; 'b*-' ;'r*-'; 'g*-'; 'c*-' ;'y.-' ;'m.-'; 'r.-' ;'g.-'; 'c.-'];

% color_map = colormap('jet');
% color_index = color_map([1:3:64],:);

figure(1);
%for i = 1:length(LT)-1,
for i = 2:2,
    hold on;
%errorbar(LAG*(24*60),RT_matrix(i,:)',RT_error(i,:)','Color',color_index(i),'LineWidth', 2,'MarkerSize',30);
errorbar(LAG*(24*60),RT_matrix(i,:)',RT_error(i,:)','Color',color_index(qq),'LineWidth', 2,'MarkerSize',30);
hold on;
axis([LAG(1)*24*60 LAG(end)*24*60 0 1]);
end;
%legend(SS);

figure(2);
%for i = 1:length(LT)-1,
for i = 2:2,
    hold on;
%plot(LAG*(24*60),lt_count(i,:)','Color',color_index(i),'LineWidth', 2,'MarkerSize',30);
plot(LAG*(24*60),lt_count(i,:)','Color',color_index(qq),'LineWidth', 2,'MarkerSize',30);
hold on;
end;
%legend(SS);
%qq = qq+1;